var express = require('express');
const jwt = require("jsonwebtoken")//jwt
const axios = require('axios')//axios
const config = require('../config/config')//配置文件 数据库连接 及 小程序appId等
const bcryptjs = require('bcryptjs')
const {UserModel}=require('../models/user');
const {goodUserModel}=require('../models/goodUser')
var Mock = require('mockjs')
var router = express.Router();



/* GET users listing. */
router.get('/', function(req, res, next) {
 
  res.send('respond with a resource');
 
});

  // 注册账号
router.post('/register',(req,res)=>{
  console.log(req.body);
  const { username, password, root } = req.body
  UserModel.create({ username, password, root }, (err,doc1) => {
      console.log(err);
      if (!err) {
          res.send({
              code: 0,
              msg: '注册成功'
          })
      } else {
          res.send({
              code: 1,
              msg: '用户名已存在'
          })
      }
  })
})
  //  登录
router.post('/logins',(req,res)=>{
      console.log(req.body);
     let {username,password}=req.body
     UserModel.findOne({username},{password:false},(err,doc)=>{
            if(doc){
              res.send(doc)
            }
     })
})


var data = Mock.mock({
  // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
  'list|1-10': [{
      // 属性 id 是一个自增数，起始值为 1，每次增 1
      "id|+1": 1,
      "roleName|1": ["测试角色1", "测试角色2", "测试角色3", "测试角色4", "测试角色5","测试角色6","测试角色7","测试角色8","测试角色9"],
      "county":"@county(true)",
      "number|1": ["1-3年","1-2年","2-3年","3-5年","5-10年"],
      "education|1":["大专","本科","高中","硕仕","初中"],
      "enterprise|1":["千库科技有限公司","北京小库科技有限公司","成都维音信息技术有限公司","百耀科技有限公司","万维科技有限公司","中通信通投资有限公司"],
       "Delivery|1":"投递",
      "createTime": Date.now,
      "salary|1":["9-12K","4-6K","5-7K","8-12K","3-6K","1.1-1.2K","1.5-2.2K"],
      "scale|1":["电子商务,移动互联网,500-1000人","电子商务,移动互联网,50-200人","电子商务,移动互联网,100-500人","电子商务,移动互联网,200-600人"],
      "image": "@image('100x100', '@color', '#fff', 'png', 'Mock.js')",   
      "Information": "@cparagraph(2)",
      "culture":"@cparagraph(5)",
      "resume": [
        {
            "username": "吴晓勇",
            "work": "6年工作经验",
            "Age": "30岁",
            "education": "本科",
            "advantage": "3年工作经验，参与并制作了很多互联网产品0到1的过程,主要设计领域为互联网金融",
            "status": "离职",
            "expect": "web前端9-12K",
            "address": "北京",
            "industry": "行业不限"
        }
    ],
  }]
})
// 输出结果
// console.log(JSON.stringify(data, null, 4))

  
// 增加职位
router.post('/MerchantPosition',(req,res)=>{
         console.log(req.body);
         data.list.forEach(item=>{
          goodUserModel.create(item,(err,doc)=>{
            console.log(doc);
         if(doc){
          res.send({
            code:200,
            msg:doc
          })
         }else{
           res.send({
             code:400,
             msg:"创建失败"
           })
         }

 })  
    
      
  })
      

})

// 获取职位详情
  router.post('/getPositionDetails',(req,res)=>{
           console.log(req.body);
          let {id}=req.body
        
          goodUserModel.findOne({_id:id},(err,doc)=>{
                  console.log(doc);
                  if(doc){
                      res.send({
                        code:200,
                        reslut:doc
                      })
                  }else{
                    res.send({
                      code:400,
                      msg:"查询失败"
                    })
                  }
          })


  })



// 搜索职位
router.post('/serchPosition',(req,res)=>{
  console.log(req.body);
  let {keywords}=req.body
     if(keywords){
      goodUserModel.find({},(err,doc)=>{
        if(!err){
          let arr=[]
          doc.forEach(item=>{
                if(item.roleName.includes(keywords)||
                item.education.includes(keywords)||
                item.enterprise.includes(keywords)||
                item.county.includes(keywords)||
                item.scale.includes(keywords)
                ){
                  console.log(item);
                  arr.push(item)
                }
          })
          res.send({
            code:200,
            success:arr
          })
        }
    })
     }else{
       res.send({
         code:400,
         msg:"查询失败"
       })
     }


})

  // 筛选职位
  router.post('/CityScreen',(req,res)=>{
          let {keywords,city,salary}=req.body
          console.log(keywords,city,salary);
            let arr3=[]
          goodUserModel.find({},(err,doc)=>{
            if(!err){
              let arr=[]
              doc.forEach(item=>{
                    if(item.roleName.includes(keywords)||
                    item.education.includes(keywords)||
                    item.enterprise.includes(keywords)||
                    item.county.includes(keywords)||
                    item.scale.includes(keywords)
                    ){
                      console.log(item);
                      arr.push(item)
                    }
              })
            if(city=="全国"&&salary=="薪资"){
                     res.send({
                       code:200,
                       success:arr
                     })
            }else if(city!=="全国"&&salary=="薪资"){
              let arr2=[]
                arr.forEach(item1=>{
                  console.log(item1.county.includes(city));
                   if(item1.county.includes(city)){
                     console.log(111);
                     arr2.push(item1)
                                  
                   }
                })
                if(arr2.length>0){
                  res.send({
                    code:200,
                    success:arr2
                  })
                }else{
                  res.send({
                    code:400,
                    msg:"暂无职位"
                  })
                }
                
            }else if(city!=="全国"&&salary!=="薪资"){
                    console.log(2222);
                    let arr3=[]
                    arr.forEach(item2=>{
                    
                      if(item2.county.includes(city)&&item2.salary.includes(salary)){
                        console.log(111);
                        arr3.push(item2)
                        res.send({
                         code:200,
                         success:arr3
                       })
                      
                      }
                   })
                 
            }else{
              res.send({
                code:400,
                info:"未查询到职位"
              })
            }
          
            }
        })
       
  })







  // 获取全部职位
router.get('/getAllPosition',(req,res)=>{

goodUserModel.find({},(err,doc)=>{
         if(!err){
            res.send({
              code:200,
              success:doc
            })
         }else{
          res.send({
            code:400,
            err:"查询失败"
          })
         }
})
})

//修改投递状态

router.post("/getDeliveryStatus",(req,res)=>{
  let {id,delivery}=req.body
    if(delivery=='投递'){
      goodUserModel.findByIdAndUpdate({_id:id},{$set:{Delivery:'已投递'}},{new:true},(err,doc)=>{
              if(doc._id==id){
                   res.send({
                     code:200,
                     success:"投递成功"
                   })
              }
      })
    }else if(delivery=="已投递"){
          res.send({
            code:201,
            info:"该职位已投递"
          })
    }
})
obj={
  username:"吴小勇",
  work:"6年工作经验",
  Age:"30岁",
  education:"本科",
  advantage:'3年工作经验，参与并制作了很多互联网产品0到1的过程,主要设计领域为互联网金融',
  status:"离职",
  expect:"web前端9-12K",
  address:"北京",
  industry:"行业不限"
}

// 简历投递
  router.post("/SendYourResume",(req,res)=>{
              let {id}=req.body
          goodUserModel.updateOne({_id:id},{$push:{resume:obj}},(err,doc)=>{
              console.log(doc);
               if(doc){
                
               
               }
               res.send({
                 code:200,
                
               })
          })
  })

// 增加简历
  router.post("/AddResume",(req,res)=>{
            let {id}=req.body
        UserModel.findOne({_id:id},(err,doc)=>{
                if(doc){
                  UserModel.updateOne({_id:id},{$push:{resume:obj}},(err,doc1)=>{
                       if(doc1){
                         res.send({
                           code:200,
                           reslut:"增加简历成功"
                         })
                       }else{
                        res.send({
                          code:400,
                          msg:"增加简历失败"
                        })
                       }
                  }) 
                }
        })
  })




module.exports = router;
